<template>
  <div class="container">
    <van-swipe class="my-swiper" :autoplay="3000" indicator-color="transparent" @change="onChange">
      <van-swipe-item v-for="(item,index) in detail.exhibit_imgs" :key="index">
        <img v-lazy="item" />
      </van-swipe-item>
      <template #indicator>
        <div class="custom-indicator">{{ current + 1 }}/4</div>
      </template>
    </van-swipe>
    <div class="audio" v-if="detail.audio">
      <div class="btn" :class="{'active':isPlay}" @click="playAudio"></div>
      <span>{{timeFormat(currentTime)}}</span>
      <div class="progress">
        <van-slider
          v-model="currentTime"
          @change="progressSlide"
          class="slider"
          bar-height="2px"
          :max="parseInt(total)==0?100:parseInt(total)"
          active-color="linear-gradient(84deg,#eab352,#ffc662,#eab352)"
          button-size="16px"
        >
          <template #button>
            <div class="custom-button"></div>
          </template>
        </van-slider>
      </div>
      <span>{{timeFormat(total)}}</span>
      <audio
        :src="detail.audio"
        v-if="detail.audio"
        class="audio"
        ref="myaudio"
        @timeupdate="audioTimeupdate"
        @ended="audioEnded"
      ></audio>
    </div>
    <div class="contents">
      <h3>{{detail.exhibit_name}}</h3>
      <div class="content-inner" v-html="detail.content"></div>
    </div>
    <div class="wrapper">
      <h4 v-if="list.length>0">展项推荐 /</h4>
      <div class="content" v-if="list.length>0">
        <div
          class="item"
          v-for="(item,index) in list"
          :key="index"
          @click="change(item.exhibit_id)"
        >
          <div class="img">
            <img v-lazy="item.exhibit_list_img" alt />
          </div>
          <div class="inner">
            <h3>{{item.exhibit_name}}</h3>
            <p>{{item.exhibition_name}} {{item.floor_name}}</p>
            <p>
              {{item.g_name}}
              <span>
                <i class="iconfont iconyanjing"></i>
                {{item.look_num}}
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      detail: "",
      current: 0,
      id: 0,
      audios: "",
      isPlay: false,
      total: 0,
      list: [],
      currentTime: 0,
      img:
        "https://www.hymuseum.org.cn/uploadfiles/article_img/20200306/202003061029236624.png"
    };
  },
  methods: {
    getDetail() {
      this.$api.getExhibitInfo(this.id).then(res => {
        this.detail = res.data.curr;
        this.list = res.data.rela;
        if (res.data.curr.audio) {
          this.$nextTick(() => {
            this.audios = this.$refs.myaudio;
            this.currentTime = this.audios.currentTime || 0;
            this.total = this.audios.duration || 0;
          });
        }
      });
    },
    /*播放结束*/
    audioEnded: function() {
      let vm = this;
      vm.isPlay = false;
    },
    change(id) {
      this.$router.push({
        path: "/exhibit/detail",
        query: {
          id: id
        }
      });
      this.id = id;
      this.getDetail();
    },
    audioTimeupdate() {},
    playAudio() {
      if (!this.isPlay) {
        setTimeout(() => {
          this.audios.play();
          this.currentTime = this.audios.currentTime || 0;
          this.total = this.audios.duration || 0;

          this.isPlay = true;
        }, 100);
      } else {
        this.audios.pause();
        this.isPlay = false;
      }
    },
    /*播放进度*/
    audioTimeupdate: function() {
      let vm = this;
      vm.currentTime = this.audios.currentTime || 0;
      vm.total = this.audios.duration || 0;
    },
    progressSlide(value) {
      this.currentTime = value;
      this.audios.currentTime = value;
      this.isPlay = true;
      this.audios.play();
    },
    timeFormat: function(t) {
      t = Math.ceil(t);
      let minute = parseInt(t / 60);
      let second = parseInt(t % 60);
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      return minute + ":" + second;
    },
    /*播放结束*/
    audioEnded: function() {
      let vm = this;
      this.currentTime = 0;
      vm.isPlay = false;
    },
    onChange(index) {
      this.current = index;
    }
  },
  mounted() {
    this.id = this.$route.query.id;
    this.getDetail();
  }
};
</script>

<style lang="scss" scoped>
// .container {
//   background: linear-gradient(#fff, #eaeaeb);
// }
.my-swiper {
  height: 375px;
  width: 100%;
  img {
    width: 100%;
    height: 100%;
  }
  .custom-indicator {
    position: absolute;
    right: 5px;
    bottom: 5px;
    font-size: 12px;
    width: 54px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    background: rgba(0, 0, 0, 0.9);
    color: #fff;
    border-radius: 9px;
  }
}
.audio {
  padding: 10px;
  display: flex;
  line-height: 40px;
  .btn {
    width: 40px;
    height: 40px;
    background: url(../.././img/play.png) no-repeat;
    background-size: cover;
    &.active {
      background: url(../.././img/pause.png) no-repeat;
      background-size: cover;
    }
  }
  .custom-button {
    width: 10px;
    height: 10px;
    background: rgba(245, 189, 90, 0.5);
    border-radius: 50%;
  }
  span {
    display: inline-block;
    color: #000;
    margin: 0 0 0 15px;
  }
  .progress {
    flex: 1;
    line-height: 40px;
    margin: 0 15px;
    position: relative;
    .slider {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
.contents {
  padding: 0 10px;
  h3 {
    font-size: 15px;
    color: #000;
    margin-bottom: 20px;
  }
  .content-inner {
    font-size: 13px;
    color: #000;
    line-height: 24px;
    margin-bottom: 20px;
  }
}
.wrapper {
  padding: 0 10px 20px;
  background: linear-gradient(#fff, #eaeaeb);
  h4 {
    font-size: 15px;
    color: #000;
    background: url(../.././img/exhibit.png) no-repeat;
    background-size: contain;
    padding-left: 20px;
    background-position: center left;
  }
  .content {
    overflow: hidden;
    padding: 10px 0;
    .item {
      width: 172px;
      background: #fff;
      border-radius: 3px;
      margin: 0px 0px 10px;
      float: left;
      overflow: hidden;
      margin-right: 10px;
      &:nth-child(2n) {
        margin-right: 0;
      }
      .img {
        width: 172px;
        height: 172px;
        overflow: hidden;
        img {
          width: 100%;
          display: block;
        }
      }
      .inner {
        padding: 10px 15px 0;
        h3 {
          color: #353535;
          font-size: 15px;
          margin-bottom: 12px;
        }
        p {
          font-size: 12px;
          line-height: 14px;
          color: #7b7c7d;
          overflow: hidden;
          margin-bottom: 9px;
          span {
            float: right;
            line-height: 18px;
            margin-top: -4px;
            i {
              font-size: 18px;
              display: inline-block;
              margin-right: 3px;
            }
          }
        }
      }
    }
  }
}
</style>